// byte[][] m_abGroundLevel3;
// int[][]  m_abItemMapLevel3;
// LayerManager m_lmLayerManagerLevel3;
// TiledLayer m_tlTiledLayerLevel3;
// public Monster m_monster, m_monster2;
//Sprite m_sprMonster; da dinh nghia o lv 1
// TiledLayer m_tlSoftwallLayerLevel3;
// Image[] itemImage;
// int frameSequence[] = {0, 0, 0, 0};
// int m_iViewX = 10, m_iViewY = 10;
public static int[][] m_aiMapItemLevel3;
public byte[][] m_abBackGroundLevel3;
public static TiledLayer tlBackGroundLevel3, tlItemLevel3;
int iLevelMapSizeW3=30;
int iLevelMapSizeH3=30;
int iMonsterLevel3=5,iMonsterLevel33=5;
//int iTimeNextLevel=50;
int[] aiOrderDraw3;
boolean bDrawHero3 = true;
//public Sprite m_sprMonster2_1, m_sprMonster2_2, m_sprMonster2_3, m_sprMonster2_4, m_sprMonster2_5;
public Monster[] aMonsterLevel3=new Monster[iMonsterLevel3];
public int[] aiItemLevel3 = {22,19,2,10,28,28,1,1,5,28,7,107,8,22,1,1,
							17,12,7,107, 28,15,2,10, 19,8,2,10, 9,5,7,107,
							15,8,2,10,23,5,2,10,
							27,3,7,107};
void UpdateLevel3(int mode)
{
	if (mode == UpdateType.UPDATE)
	{
	//control main chacracter
		// s_MainCharacter.Move(m_aiMapItemLevel1);
		// s_MainCharacter.DecreaseBlood(m_aiMapItemLevel1);
		
	//AI for monster
			
		// for(int i=0; i< aMonsterLevel1.length; i++)
		// {	
			// if(aMonsterLevel1[i] ==null) continue;
			// aMonsterLevel1[i].Move(m_aiMapItemLevel1);
			// aMonsterLevel1[i].DecreaseBlood(m_aiMapItemLevel1);
			// if(aMonsterLevel1[i].CheckDie())
			// {				
				// if(--(aMonsterLevel1[i].iTimeDie)>0)
				// {					
					// aMonsterLevel1[i].m_iDir=4;
					// aMonsterLevel1[i].iMove=4;
				// }
				// else
				// {
				// aMonsterLevel1[i]=null;			
				// }
			// }				
		// }
		
	// put boom
		// if (IsKeyPressed(GK_SELECT)) 
			// {		
				// s_MainCharacter.PutBomb(m_aiMapItemLevel1, tlItemLevel1);
			// }
		
	// check bom explosion
		// BombExplosion(m_aiMapItemLevel1, tlItemLevel1);
	
	// set view 
		// iViewX=s_MainCharacter.m_iCharacterX-VIEW_W/2;
		// iViewY=s_MainCharacter.m_iCharacterY-VIEW_H/2;
		// if(iViewX<0)
			// iViewX=0;
		// else if(iViewX>560)
			// iViewX=560;
		// if(iViewY<0)
			// iViewY=0;
		// else if(iViewY>200)
			// iViewY=200;
				// s_lmMap.setViewWindow(iViewX, iViewY, VIEW_W, VIEW_H);
	
	// check main character die
		// if(s_MainCharacter.CheckDie())
		// {
			// if(--s_MainCharacter.s_iLife >0)
				// SwitchState(GameState.NextLevel);
			// else 
				// SwitchState(GameState.GameOver);
		// }
		  if (IsKeyPressed(GK_SELECT)) 
			{		
				if(s_MainCharacter.PutBomb(m_aiMapItemLevel3, tlItemLevel3)==1)
				{
					PlaySound("/putBom.wav",1);
				}
			}
	//control main chacracter
		s_MainCharacter.Move(m_aiMapItemLevel3);
		s_MainCharacter.DecreaseBlood(m_aiMapItemLevel3);
		
	//AI for monster		
		for(int i=0; i< aMonsterLevel3.length; i++)
		{	
			if(aMonsterLevel3[i] ==null) continue;
			aMonsterLevel3[i].Move(m_aiMapItemLevel3);
			aMonsterLevel3[i].DecreaseBlood(m_aiMapItemLevel3);
			//monster die
			if(aMonsterLevel3[i].CheckDie())
			{				
				if(--(aMonsterLevel3[i].iTimeDie)>0)
				{					
					aMonsterLevel3[i].m_iDir=4;
					aMonsterLevel3[i].iMove=4;
				}
				else
				{
				m_aiMapItemLevel3[aMonsterLevel3[i].iInMapY][aMonsterLevel3[i].iInMapX]=rand.nextInt(6)+107;
				System.out.println("Item: "+m_aiMapItemLevel3[aMonsterLevel3[i].iInMapY][aMonsterLevel3[i].iInMapX]);
				aMonsterLevel3[i]=null;	
				iMonsterLevel33--;
				}
			}				
		}
		if(iTimeNextLevel<=0) //change to next level
		{
			s_iLevel=2;
			//play sound here
			try
			{
				Thread.sleep(1000);
			}catch(Exception e){}
			SwitchState(GameState.NextLevel);
		}		
	//put boom
		
		if (IsKeyPressed(DK_NUM0))
		{
			s_MainCharacter.ChangeBoom();
		}		
	//check bom explosion
		BombExplosion(m_aiMapItemLevel3, tlItemLevel3);
	
	//set view 
		iViewX=s_MainCharacter.m_iCharacterX-VIEW_W/2;
		iViewY=s_MainCharacter.m_iCharacterY-VIEW_H/2;
		if(iViewX<0)
			iViewX=0;
		else if(iViewX>560)
			iViewX=560;
		if(iViewY<0)
			iViewY=0;
		else if(iViewY>520)
			iViewY=520;
				s_lmMap.setViewWindow(iViewX, iViewY, VIEW_W, VIEW_H);
	
	//check main character die
		if(s_MainCharacter.CheckDie())
		{
			//play sound here
			try
			{
				Thread.sleep(1000);
			}catch(Exception e){}
			if(--s_MainCharacter.s_iLife >0)
				SwitchState(GameState.NextLevel);
			else 
				SwitchState(GameState.GameOver);
		}
		if (IsKeyPressed(DK_SOFT_LEFT))
		SwitchState1(GameState.InGameMenu);
	} //end of Update mode
	
	if (mode == UpdateType.PAINT)
	{		
		// s_g.setColor(c_white);
		// s_g.fillRect(0, 0, SCREEN_W, SCREEN_H);
        //draw background
		// s_lmMap.paint(s_g, 0, 0);
		//draw monster and main character
		// for(int i=0; i< aMonsterLevel1.length; i++)
		// {	
			// if(aMonsterLevel1[i] ==null) continue;
			// if(aMonsterLevel1[i].iDepth > s_MainCharacter.iDepth)continue;
			// aMonsterLevel1[i].DrawSprite();
		// }
		// s_MainCharacter.DrawSprite();
		// for(int i=0; i< aMonsterLevel1.length; i++)
		// {	
			// if(aMonsterLevel1[i] ==null) continue;
			// if(aMonsterLevel1[i].iDepth <= s_MainCharacter.iDepth)continue;
			// aMonsterLevel1[i].DrawSprite();
		// }		
        s_g.setColor(c_white);
		s_g.fillRect(0, 0, SCREEN_W, SCREEN_H);
	
	//draw background
		s_lmMap.paint(s_g, 0, 0);
		
	//draw monster and main character
		bDrawHero3= true;
		for(int i=0; i< aMonsterLevel3.length; i++)
		{	
		//get depth	
			if(aMonsterLevel3[i] ==null) continue;
			aiOrderDraw3[i]=aMonsterLevel3[i].iDepth;
			aiOrderDraw3[iMonsterLevel3+i]=i;			
		}	
		Sort(aiOrderDraw3);				 
		for(int i=0; i< aMonsterLevel3.length; i++)
		{	
			if(	aMonsterLevel3[aiOrderDraw3[i+iMonsterLevel3]] ==null) continue;	
			if(aMonsterLevel3[aiOrderDraw3[i+iMonsterLevel3]].iDepth > s_MainCharacter.iDepth && bDrawHero3)
			{
				s_MainCharacter.DrawSprite();
				bDrawHero3 = false;
			}
			aMonsterLevel3[aiOrderDraw3[i+iMonsterLevel3]].DrawSprite();
			aMonsterLevel3[aiOrderDraw3[i+iMonsterLevel3]].DrawHeathBar();			
		}
		if(bDrawHero3)
			{
				s_MainCharacter.DrawSprite();
				bDrawHero3 = false;
			}
		
	//check all monster was destroy
	if(iMonsterLevel33<=0)
	{
		iTimeNextLevel--;
		s_g.drawImage(imgWin, m_iMidX, m_iMidY-20, Graphics.HCENTER | Graphics.VCENTER);
	}
	}//end of PAINT mode
	
	if (mode == UpdateType.CTOR)
	{
		try
		{
			// iViewX=100;
			// iViewY=100;
			// m_abBackGroundLevel1=ReadFileTiles("/boss8bit1.dat", 30, iLevelMapSizeH);			
			// tlBackGroundLevel1 = new TiledLayer(30, iLevelMapSizeH, imgInGameTile, 16, 16);		
			// if(tlBackGroundLevel1!=null)
			// System.out.println("TiledLayer create success !");			
			// CreateTiles(m_abBackGroundLevel1, tlBackGroundLevel1, 30, 30);
			// tlItemLevel1 = new TiledLayer(30, iLevelMapSizeH, imgItemTile, 16, 16);	
			// m_aiMapItemLevel1=new int[30][30];
			// CreateMapItem(m_abBackGroundLevel1, m_aiMapItemLevel1);
			// s_MainCharacter.SetPosition(25, 15);		
			// monsterImage = new Image[5];
			// monsterImage[0] = Image.createImage("/monster1_strip40.png"); // This image is use to paint monster
			// System.out.println("Initialize NPC...");
			// if(monsterImage == null)System.out.println("Error load img for monster");
			// m_sprMonster1 = new Sprite(monsterImage[0], 24, 24);	
			// System.out.println("Pass: Initialize the monster");
			// aMonsterLevel1[0]= new Monster(m_sprMonster1, 21, 13);
			// aMonsterLevel1[1]= new Monster(m_sprMonster1, 28, 13);
			// aMonsterLevel1[0].m_iAI = 2;
			// aMonsterLevel1[1].m_iAI = 2;
			// s_lmMap.append(tlItemLevel1);
			// s_lmMap.append(tlBackGroundLevel1);
			iViewX=100;
			iViewY=100;
			//tiledlayer for background
			m_abBackGroundLevel3=ReadFileTiles("/boss8bit1.dat", iLevelMapSizeW3, iLevelMapSizeH3);			
			tlBackGroundLevel3 = new TiledLayer(iLevelMapSizeW3, iLevelMapSizeH3, imgInGameTile, 16, 16);		
			if(tlBackGroundLevel3!=null)
				System.out.println("TiledLayer create success !");			
			CreateTiles(m_abBackGroundLevel3, tlBackGroundLevel3, 30, 30);
			
			//For Item
			tlItemLevel3 = new TiledLayer(iLevelMapSizeW3, iLevelMapSizeH3, imgItemTile, 16, 16);	
			//1-5: soft-wall
			//6-12 Item
			//13-17 Bom
			//17-25 bom explosion
			//26-30 hit
			m_aiMapItemLevel3=new int[30][30];
			CreateMapItem(m_abBackGroundLevel3, m_aiMapItemLevel3);
		//add default Item
			AddMapItemLevel3(m_aiMapItemLevel3, tlItemLevel3);
			
			 System.out.println("Initialize NPC...");
			// Initialize Character 		
			
		//Create monster				
			if(monsterImage[0] == null)System.out.println("Error load img for monster");
			//m_sprMonster1 = new Sprite(monsterImage[0], 32, 32);			
			aMonsterLevel3[0]= new Monster(monsterImage[2], 24, 24);
			aMonsterLevel3[1]= new Monster(monsterImage[1], 24, 24);
			aMonsterLevel3[2]= new Monster(monsterImage[1], 24, 24);	
			aMonsterLevel3[3]= new Monster(monsterImage[1], 24, 24);	
			aMonsterLevel3[4]= new Monster(monsterImage[2], 24, 24);	
			
			aMonsterLevel3[0].SetPosition(22,15);
			aMonsterLevel3[1].SetPosition(17,25);
			aMonsterLevel3[2].SetPosition(24,15);
			aMonsterLevel3[2].m_iAI=2;
			aMonsterLevel3[3].SetPosition(1,14);
			aMonsterLevel3[4].SetPosition(3,3);
			// s_MainCharacter.SetPosition(25,15);			
			aMonsterLevel3[1].m_iBlood = 2;
			// aMonsterLevel1[1].m_iAI = 2;
			aMonsterLevel3[2].m_iBlood = 2;
			aMonsterLevel3[3].m_iBlood = 2;
			aMonsterLevel3[4].m_iBlood = 2;
			// s_lmMap.append(s_MainCharacter);
			// for(int i=0;i<iMonsterLevel2;i++)
		    // s_lmMap.append(aMonsterLevel2[i]);
		    
		    aiOrderDraw3= new int[iMonsterLevel3*2];
			s_lmMap.append(tlItemLevel3);
			s_lmMap.append(tlBackGroundLevel3);
			} 
		catch (Exception e)
		{
			System.out.println("Error Level3-CTOR: " + e.getMessage());
		}
				// try{Thread.sleep(21000);}catch (Exception e)	{}				
		
	}
	if (mode== UpdateType.DTOR)
	{	
		// switch(s_iLevel)
		// {
			// case 1: break;
			// case 2: break;
			// case 3: UpdateLevel3(mode); break;			
		// }
        for(int i=0;i<iMonsterLevel3;i++)aMonsterLevel3[i]=null;
		tlItemLevel3=null;
		tlBackGroundLevel3=null;
		for(int i=0;i<iMonsterLevel3;i++)s_lmMap.remove(aMonsterLevel3[i]);
		s_lmMap.remove(tlItemLevel3);
		s_lmMap.remove(tlBackGroundLevel3);		
	}		
}
public void AddMapItemLevel3(int [][] data, TiledLayer tl)
	{
		int l=aiItemLevel3.length/4;
		for(int i=0; i<l; i++)
		{
			data[aiItemLevel3[i*4+1]][aiItemLevel3[i*4]] = aiItemLevel3[i*4+3];		
			tl.setCell(aiItemLevel3[i*4],aiItemLevel3[i*4+1], aiItemLevel3[i*4+2]);				
			System.out.println(aiItemLevel3[i*4] +":" +aiItemLevel3[i*4+1] +":" +aiItemLevel3[i*4+2] +":" +aiItemLevel3[i*4+3]);
		}
		System.out.println("Created default item !");
	}


